const fs = require("fs")
const artTemplate = require("art-template")


function htmlRead(url, response) {
    fs.readFile(url, function (error, data) {
        if (error) {
            response.end(error)
        } else {
            response.end(data)
        }
    })
}
function htmlReadWithData(url, response,data1) {
    fs.readFile(url, function (error, data) {
        if (error) {
            response.setHeader("Content-Type", "text/plain;charset=UTF-8");
            response.write("文件找不到了....")
            response.end(error.toString())
        } else {
            response.end(artTemplate.render(data.toString(), data1)) 
        }
    })
}

function readFile(path) {
    return new Promise((resolve, reject) => {
        fs.readFile(path, function (error,data) {
            if (error) {
                reject(error)
            } else {
                resolve(data)
            }
        })
    })
}

function writeFile(path, content) {
    return new Promise((resolve, reject) => {
        fs.writeFile(path, content,function (error) {
            if (error) {
                reject(error)
            } else {
                resolve("保存成功")
            }
        })
    })
}

module.exports = {
    htmlRead,
    htmlReadWithData,
    readFile,
    writeFile
}